package com.wzdigit.wms.wms.task;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzdigit.mes.basic.api.BasicParamApi;
import com.wzdigit.mes.basic.dto.ParamDetailDTO;
import com.wzdigit.wms.basic.domain.asn.AsnDtl;
import com.wzdigit.wms.basic.domain.asn.AsnHeader;
import com.wzdigit.wms.basic.domain.oqc.OqcInsp;
import com.wzdigit.wms.basic.enums.AsnDtlStatusEnum;
import com.wzdigit.wms.basic.enums.AsnHeaderStatusEnum;
import com.wzdigit.wms.basic.enums.AsnTypeEnum;
import com.wzdigit.wms.basic.enums.OqcInspStatusEnum;
import com.wzdigit.wms.basic.mapper.core.oqc.OqcInspMapper;
import com.wzdigit.wms.basic.mapper.core.order.AsnDtlMapper;
import com.wzdigit.wms.basic.mapper.core.order.AsnHeaderMapper;
import com.wzdigit.wms.wms.service.core.asn.AsnDtlService;
import com.wzdigit.wms.wms.service.core.asn.AsnHeaderService;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.StringUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

//定时关闭下线收货单
@ConditionalOnExpression("${schedule.common.switch}")
@Component
@Slf4j
public class ReceiveGoodsCloseTask {

    final String userName = "系统后台任务";

    @Autowired
    private AsnHeaderMapper asnHeaderMapper;
    @Autowired
    private AsnDtlMapper asnDtlMapper;

    @Reference(version = "${dubbo.providers.basic.BasicParam.api:1.0.0}", check = false)
    BasicParamApi basicParamApi;

    //新版本
//    @Scheduled(cron = "0 0/5 * * * ?")
    @Scheduled(cron = "0 0/20 * * * ?")
    public synchronized void run(){
        //需要关闭的收货单类型：下线交收(自动&人工)
        List<String> asnTypeList = Arrays.asList(AsnTypeEnum.R01.code, AsnTypeEnum.R96.code);
        //查询截止时间
        ParamDetailDTO paramDetailDTO = basicParamApi.getByCode("MDIC_HZ");
        String rateStr=paramDetailDTO.getSystemParamValue();
        Integer rate =- Integer.valueOf(rateStr);
        //截止时间：当前时间前推rateStr分钟
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MINUTE,rate);
        Date closeTime = calendar.getTime();
        Date nowDate=new Date();
        //查询需要关闭的收货单
        List<BigDecimal> asnHeaderIds =asnHeaderMapper.searchCloseHeaderId(asnTypeList,closeTime);
        if (CollectionUtil.isNotEmpty(asnHeaderIds)){
            log.info("====定时任务: 处理自动关下线收货单 {" + StringUtil.join(asnHeaderIds,",") + "}======");
            AsnHeader asnHeader=new AsnHeader();
            AsnDtl asnDtl=new AsnDtl();
            asnHeader.setStatus(AsnHeaderStatusEnum.FINISH.getCode());
            asnHeader.setModifyUser("system");
            asnHeader.setModifyTime(nowDate);
            asnDtl.setStatus(AsnDtlStatusEnum.FINISH.getCode());
            asnDtl.setModifyUser(userName);
            asnDtl.setModifyTime(nowDate);
            //更新主档
            asnHeaderMapper.update(asnHeader,new QueryWrapper<AsnHeader>().in("ASN_HEADER_ID",asnHeaderIds));
            //更新明细
            asnDtlMapper.update(asnDtl,new QueryWrapper<AsnDtl>().in("ASN_HEADER_ID",asnHeaderIds));
        }
    }

    /**
     * 老版本
     * 每两分中运行一次检索关闭收货单
     */
//    @Scheduled(cron = "30 0/5 * * * ?")
/*    @Scheduled(cron = "0 0/20 * * * ?")
    public synchronized void run() {
        //1. 获取未关闭的送货单
        List<AsnHeader> asnHeaders = this.asnHeaderService.selectProgressOrder();
        if (asnHeaders.size() == 0) return;
        //2, 循环
        for (AsnHeader item : asnHeaders) {
            //  2.1 获取送货单明细
            List<AsnDtl> listAsnDetail = this.asnDtlService.selectListByAsnNo(item.getAsnNo());
            if (listAsnDetail.size() == 0)
                continue;
            for (AsnDtl detail : listAsnDetail) {
                //2.2 获取送检单
                OqcInsp oqcInsp = this.oqcInspMapper.selectByInspNo(detail.getInspNo());
                if (oqcInsp == null)
                    continue;
                //2.3 如果数量一致则，修改主表明细表状态
                if (!oqcInsp.getStatus().equals(OqcInspStatusEnum.OQC_STATUS_AFTER_HANDLE.getCode()))
                    continue;
                if (BigDecimal.valueOf(oqcInsp.getInspQty()).compareTo(detail.getRcvQty()) != 0)
                    continue;

                log.info("====定时任务: 处理自动关单 [" + item.getAsnNo() + "]======");

                detail.setStatus(AsnDtlStatusEnum.FINISH.getCode());
                detail.setModifyTime(new Date());
                detail.setModifyUser(userName);

                item.setStatus(AsnHeaderStatusEnum.FINISH.getCode());
                item.setModifyTime(new Date());
                item.setModifyUser(userName);

                this.asnDtlService.getMapper().updateById(detail);
                this.asnHeaderService.getMapper().updateById(item);
            }
        }
    }*/


}
